#!/usr/bin/perl

use strict;
use warnings;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $LOGFILE = "m.log";

=pod
E m4m5m8m9s1s7s9p1p3j2j2j7
S m2m3s3s4s8p4p4p5p6j1j6
W m3m5m6m6s6p1p6p7j3j5j6
N m1m3m7m8s5s6p6p9p9j5j5

Content-type: text/plain; charset=utf-8

<xml>
        <EAST>m:1,m:8,m:8,s:1,s:3,s:4,s:6,s:6,s:9,p:1,p:2,p:8</EAST>
        <RANDOM>1</RANDOM>
</xml>
に変換する！
=cut

my $query_str    = (defined $ENV{'QUERY_STRING'})? $ENV{'QUERY_STRING'} : "" ;
my $request_type = (defined $ENV{'REQUEST_METHOD'})? $ENV{'REQUEST_METHOD'} : "" ;
#my $data = new CGI;
#my $cmd   = (defined $data->param('cmd')) ? $data->param('cmd') : $ARGV[0];
#my $first = (defined $data->param('first')) ? $data->param('first') : $ARGV[1];

my %query;
foreach my $line (split /\&/, $query_str) {
	my ($key, $val) = split /\=/, $line;
	$query{$key} = $val;
	log_out("$key=$val");
}

my $cmd   = $query{'cmd'};

# header
print "Content-type: text/plain; charset=utf-8\n\n";


if ($cmd eq "init") {
	init_main();
}
elsif ($cmd eq "sute") {

	my $caret = $query{'caret'};
	my $home = $query{'home'};
	sute_main($home, $caret);
}
elsif ($cmd eq "show") {
	my $home = $query{'home'};
	show_main($home);
}
else{
	log_out("$query_str");
	print "$query_str\n";
}


exit(0);



sub init_main
{
	my $script = "./m.pl";
	
	my @list = `$script init`;
	my $exit = $?;
	print_html(\@list, $exit);
=pod	
	print "<xml>\n";
	foreach my $line (@list) {
		chomp $line;
		my ($tag, $other) = split / /, $line;
		
		if (defined $tag && $tag eq "E" || $tag eq "S" || $tag eq "W" || $tag eq "N") {
			$other =~ s/([a-z])(\d)/$1:$2,/g;
			$other =~ s/,$//;
			my $home;
			$home = "EAST"  if $tag eq "E";
			$home = "SOUTH" if $tag eq "S";
			$home = "WEST"  if $tag eq "W";
			$home = "NOUTH" if $tag eq "N";
			print "\t<$home>$other</$home>\n";
		}
		elsif (defined $tag && $tag eq "TSUMO") {
			$other =~ s/([a-z])(\d)/$1:$2,/g;
			$other =~ s/,$//;
			print "\t<$tag>$other</$tag>\n";
		}
		elsif (defined $other) {
			print "\t<$tag>$other</$tag>\n";
		}
	}
	print "\t<RANDOM>$query_str</RANDOM>\n";
	print "\t<METHOD>$request_type</METHOD>\n";
	print "\t<EXIT>$exit</EXIT>\n";
	print "</xml>\n";
=cut
	return;
}


sub sute_main
{
	my ($home, $caret) = @_;
	my $script = "./m.pl";
	
	my @list = `$script sute $home $caret`;
	my $exit = $?;
	print_html(\@list, $exit);
}

sub show_main
{
	my ($home) = @_;
	my $script = "./m.pl";
	
	my @list = `$script show $home`;
	my $exit = $?;
	print_html(\@list, $exit);
}

sub print_html
{
	my ($list, $exit) = @_;
	
	
	print "<xml>\n";
	foreach my $line (@$list) {
		chomp $line;
		my ($tag, $other) = split / /, $line;
		
		if (defined $tag && $tag eq "E" || $tag eq "S" || $tag eq "W" || $tag eq "N") {
			$other =~ s/([a-z])(\d)/$1:$2,/g;
			$other =~ s/,$//;
			my $home;
			$home = "EAST"  if $tag eq "E";
			$home = "SOUTH" if $tag eq "S";
			$home = "WEST"  if $tag eq "W";
			$home = "NOUTH" if $tag eq "N";
			print "\t<$home>$other</$home>\n";
		}
		elsif (defined $tag && $tag eq "TSUMO") {
			$other =~ s/([a-z])(\d)/$1:$2,/g;
			$other =~ s/,$//;
			print "\t<$tag>$other</$tag>\n";
		}
		elsif (defined $other) {
			print "\t<$tag>$other</$tag>\n";
		}
	}
	print "\t<RANDOM>$query_str</RANDOM>\n";
	print "\t<METHOD>$request_type</METHOD>\n";
	print "\t<EXIT>$exit</EXIT>\n";
	print "</xml>\n";
	return ;
}

sub log_out
{
	my ($msg) = @_;
	my $date = `date`;
	chomp $date;
	open my $fh, ">> $LOGFILE";
	print $fh "$date : $msg\n";
	close $fh;
}
